package com.da.javatest.interview.session2;

import java.util.concurrent.CountDownLatch;

import lombok.Getter;

/**
 * @author chenlida
 * @date 2020/3/29 22:49
 * CountDownLatch 相当于做减法，倒计时为0才能往下继续
 */
public class Test12CountDownLatch {
    public static void main(String[] args) throws InterruptedException {
        /*CountDownLatch latch = new CountDownLatch(6);

        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " 上完自习课，离开了教室");
                latch.countDown();
            }, String.valueOf(i)).start();
        }

        latch.await();
        System.out.println("班长关门关灯离开教室");*/

        CountDownLatch latch = new CountDownLatch(6);

        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " 被消灭");
                latch.countDown();
            }, CountryEnum.getById(i).getName()).start();
        }

        latch.await();
        System.out.println("秦国统一天下");
    }
}

enum CountryEnum {
    ONE(1, "齐国"),
    TWO(2, "楚国"),
    THREE(3, "燕国"),
    FOUR(4, "赵国"),
    FIVE(5, "魏国"),
    SIX(6, "韩国"),
    ;
    @Getter
    private Integer id;
    @Getter
    private String name;

    CountryEnum(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public static CountryEnum getById(int id) {
        for (CountryEnum value : CountryEnum.values()) {
            if (value.getId() == id) {
                return value;
            }
        }
        return null;
    }
}
